package com.chenglai.goods.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.goods.pojo.ProductComment;
import com.chenglai.goods.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jiang
 * @created 2023/11/16 10:04:03
 **/
@CrossOrigin
@RestController
@RequestMapping("/comment")
@Api(value = "/comment", tags = {"商品评论服务"})
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/addProductComment")
    @ApiOperation(value = "添加商品评论")
    public Result addProductComment(ProductComment productComment){
        try {
            commentService.addProductComment(productComment);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @GetMapping("/addProductCommentPic")
    @ApiOperation(value = "添加商品评论图片")
    public Result addProductCommentPic(HttpServletRequest request){
        try {
            String pic = commentService.addProductCommentPic(request);
            return new Result(true, StatusCode.OK,"操作成功",pic);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @GetMapping("/getProductComment")
    @ApiOperation(value = "获取商品评论")
    public Result getProductComment(@ApiParam(name = "productId", value = "商品ID") @RequestParam("productId")Integer productId){
        try {
            List<ProductComment> productCommentList = commentService.getProductComment(productId);
            return new Result(true, StatusCode.OK,"操作成功",productCommentList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @GetMapping("/delProductComment")
    @ApiOperation(value = "用户删除商品评论")
    public Result delProductComment(@ApiParam(name = "commentId", value = "评论Id") @RequestParam("commentId")Integer commentId,
                                    @ApiParam(name = "pics", value = "评论图片多个,号分割 没有传''") @RequestParam("pics")String pics){
        try {
            commentService.delProductComment(commentId,pics);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }


}
